/**
 * Created by WLG on 2018/11/20.
 */

let ChatService = require("./chatService");
let ChatMsgService = require("./chatMsgService");
let ChatMemInfoService = require("./chatMemInfoService");
let AccountMemService = require("./accountMemService");
/**
 * 为解决循环依赖问题 ，凡是需要多个基础service引用的方法 提升到 businessService中
 */
class ChatBusinessService{
    //chat
    static async getChatCacheAndMemInfo(id){
        let tempChat = await ChatService.getCache(id);
        if(tempChat){
            let memArray = tempChat.mem;
            let memMap = {};
            if(memArray){
                for(let i = 0 ; i<memArray.length; i++){
                    let tempID = memArray[i];
                    memMap[tempID] = await AccountMemService.getCache(tempID);
                }
            }
            if(tempChat._doc){
                tempChat._doc.memMap = memMap;
            }else{
                tempChat.memMap = memMap;
            }
        }
        return tempChat;
    }
    //ChatMemInfoService
    /**
     * 获取聊天列表
     * 每一条目 包含 chat chatMemInfo 两部分
     * @param memId
     * @param lastTime
     * @returns {Promise.<Array>}
     */
    static async searchChatAndChatMemByMemId(memId,lastTime){
        let chatMemInfoList = await ChatMemInfoService.searchByMemId(memId,lastTime);
        let resultArray = [];
        for(let i = 0 ; i<chatMemInfoList.length ; i++){
            resultArray.push({
                chat:await this.getChatCacheAndMemInfo(chatMemInfoList[i].chatID),
                chatMemInfo: chatMemInfoList[i],
                lastChatMsg:await ChatMsgService.getLastChatMsgCache(chatMemInfoList[i].chatID)
            });
        }
        return resultArray;
    }
    //getOneChatAndChatMemInfo
    /**
     * 获取单条会话信息
     * 包含 chat chatMemInfo 两部分
     * @param memId
     * @param lastTime
     * @returns {Promise.<Array>}
     */
    static async getOneChatAndChatMemInfo(memId , chatId ){
        return {
            chat: await this.getChatCacheAndMemInfo(chatId),
            chatMemInfo: await ChatMemInfoService.getCache(memId,chatId),
            lastChatMsg: await ChatMsgService.getLastChatMsgCache(chatId)
        };
    }
    //chatMsgService
    /**
     * 获取聊天详情
     * @param memID
     * @param chatID
     * @param lastSortID
     * @returns {Promise.<{chat: *, chatInfo: Promise, chatMsgList: *}>}
     */
    static async getChatMsgDetail(memID,chatID,lastSortID){
        //console.log(await this.getChatCacheAndMemInfo(chatID));
        let tempVal = await this.getChatCacheAndMemInfo(chatID);
        return {
            chat: tempVal,
            chatMemInfo: await ChatMemInfoService.getCache(memID, chatID),
            chatMsgList: await ChatMsgService.searchChatMsgList(chatID,lastSortID)
        };
    }

    /**
     * 创建会话
     * @param item
     * @returns {Promise.<void>}
     */
    static async createChat(item){
        item.createTime = new Date().getTime();
        item.updateTime = new Date().getTime();
        item.status="1";
        return await ChatService.save(item);
    }

    /**
     * 根据人名搜索
     * @param item
     * @returns {Promise.<void>}
     */
    static  async searchAccountMem(item){
        return await AccountMemService.searchMemByName(item);
    }

    /**
     * 根据chatID查询群文件
     * @param item
     * @returns {Promise.<*>}
     */
    static async searchGroupFile(item){
        return await ChatMsgService.searchImageFile(item);
    }

    /**
     * 撤回消息
     * @param charID
     * @param chatMsgID
     * @returns {Promise.<*>}
     */
    static async withdrawMsg(charID,chatMsgID){
        await ChatMsgService.delMsg(chatMsgID);
        return await ChatMsgService.getLastChatMsgCache(charID,true);
    }

}
module.exports = ChatBusinessService;